<?php

namespace plugin\cms\app\bll;

use Exception;
use plugin\cms\app\enum\ContentStatus;
use support\Db;

class ContentPending
{

    /**
     * 审核内容
     *
     * @param integer $contentid
     * @param integer $cid
     * @param integer $siteid
     * @param integer $status
     * @param integer $pendinguid
     * @param string $opinion
     * @return void
     */
    public function pending(
        int $contentid,
        int $cid,
        int $siteid,
        int $status,
        int $pendinguid,
        string $opinion = ''
    ) {
        Db::beginTransaction();
        try {

            $bllContent = new Content();
            $contentinfo = $bllContent->find(id: $contentid, cid: $cid, siteid: $siteid);
            if (!$contentinfo) {
                throw new  Exception('文章不存在');
            }
            //插入审核表
            Db::table('cms_content_pending')
                ->insert([
                    'contentid'   => $contentid,
                    'cid'         => $cid,
                    'status'      => $status,
                    'pendinguid'  => $pendinguid,
                    'opinion'     => $opinion,
                    'siteid'      => $siteid,
                    'pendingtime' => date('Y-m-d H:i:s'),
                ]);

            //修改内容状态
            $bllCategory = new Category();
            $tablename = $bllCategory->getTableName($cid, $siteid);
            Db::table($tablename)->where('id', '=', $contentid)
                ->where('status', '=', 0)
                ->update([
                    'status' => $status,
                ]);

            $bllCategory->computeContentCountAtCid($cid, $siteid);

            Db::commit();
        } catch (Exception $ex) {
            Db::rollBack();
            throw $ex;
        }
    }


    /**
     * 批量审核
     *
     * @param string|array $contentids
     * @param integer $cid
     * @param integer $siteid
     * @param integer $status
     * @param integer $pendinguid
     * @param string $opinion
     * @return void
     */
    public function batchPending(
        string|array $contentids,
        int $cid,
        int $siteid,
        int $status,
        int $pendinguid,
        string $opinion = ''
    ) {
        Db::beginTransaction();
        try {
            if (is_string($contentids)) {
                $contentids = explode(',', $contentids);
            }
            $pendingDatas = [];
            foreach ($contentids as $contentid) {
                $pendingDatas[] = [
                    'contentid'   => $contentid,
                    'cid'         => $cid,
                    'status'      => $status,
                    'pendinguid'  => $pendinguid,
                    'opinion'     => $opinion,
                    'siteid'      => $siteid,
                    'pendingtime' => date('Y-m-d H:i:s'),
                ];
            }
            Db::table('cms_content_pending')->insert($pendingDatas);

            //修改内容状态
            $bllCategory = new Category();
            $tablename = $bllCategory->getTableName($cid, $siteid);
            Db::table($tablename)->whereIn('id', $contentids)
                ->where('status', '=', 0)
                ->update([
                    'status' => $status,
                ]);

            $bllCategory->computeContentCountAtCid($cid, $siteid);
            Db::commit();
        } catch (Exception $ex) {
            Db::rollBack();
            throw $ex;
        }
    }


    /**
     * 最后一条未通过记录
     *
     * @param integer $contentid
     * @param integer $cid
     * @param integer $siteid
     * @return array
     */
    public function LastUnpassRecord(int $contentid, int $cid, int $siteid): array
    {
        $pendinginfo = Db::table('cms_content_pending')
            ->where('contentid', '=', $contentid)
            ->where('cid', '=', $cid)
            ->where('siteid', '=', $siteid)
            ->where('status', '=', ContentStatus::STATUS_UNPASS->value)
            ->first();
        return $pendinginfo === null ? [] : get_object_vars($pendinginfo);
    }
}
